package com.dating.core.ext;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.Encoding;
import org.nutz.mvc.view.ForwardView;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.ServerRedirectView;
import org.nutz.mvc.view.UTF8JsonView;

import com.dating.base.dto.Pager;
import com.dating.utils.StrKit;

@Encoding(input = "UTF-8", output = "UTF-8")
@IocBean
public abstract class BaseModule {

	protected static final Log log = Logs.get();

	protected void _addCookie(String name, String value, int age) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		cookie.setMaxAge(age);
		Mvcs.getResp().addCookie(cookie);
	}

	public String _base() {
		return Mvcs.getReq().getContextPath();
	}

	public String _ua() {
		return Mvcs.getReq().getHeader("user-agent");
	}

	public int _fixPage(int page) {
		return ((page <= 0) ? 1 : page);
	}

	public String _fixSearchKey(String key) {
		HttpServletRequest request = Mvcs.getReq();
		if ((Strings.equalsIgnoreCase("get", request.getMethod()))
				&& (Lang.isWin())) {
			key = (Strings.isBlank(key)) ? "" : key;
			try {
				return new String(key.getBytes("iso-8859-1"),
						request.getCharacterEncoding());
			} catch (UnsupportedEncodingException e) {
				log.debug(e);
				return "";
			}
		}
		return ((Strings.isBlank(key)) ? "" : key);
	}

	protected String _getCookie(String name) {
		Cookie[] cookies = Mvcs.getActionContext().getRequest().getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (Strings.equals(cookie.getName(), name)) {
					return cookie.getValue();
				}
			}
		}
		return null;
	}

	public abstract String _getNameSpace();

	public String _ip() {
		return Lang.getIP(Mvcs.getReq());
	}

	protected void _putSession(String key, Object value) {
		Mvcs.getReq().getSession().setAttribute(key, value);
	}

	public View _renderForward(String path, Object[] objs) {
		Mvcs.getActionContext().getRequest().setAttribute("objs", objs);
		return new ForwardView(path);
	}

	public View _renderJson(Object[] objs) {
		UTF8JsonView view = (UTF8JsonView) UTF8JsonView.NICE;
		view.setData(objs);
		return view;
	}

	public View _renderJsp(String path, Object[] objs) {
		Mvcs.getActionContext().getRequest().setAttribute("objs", objs);
		return new JspView(path);
	}

	public View _renderRedirct(String path) {
		return new ServerRedirectView(path);
	}
	
	/**
	 * 获取参数key值
	 * @param key
	 * @return
	 */
	public String getPara(String key){
		return Mvcs.getReq().getParameter(key);
	}
	
	
	/**
	 * 取指定个数参数keys的参数值
	 * @param keys
	 * @return
	 */
	public Map<String, Object> getParas(String ...keys) {
		HttpServletRequest request = Mvcs.getReq();
		Map<String, Object> params = new HashMap<String, Object>();
		for (String key : keys) {
			String val = request.getParameter(key) ;
			if(StrKit.notBlank(val)){
				params.put(key, val);
			}
		}
		return params ;
	}
	
	/**
	 * 创建pager对象
	 * @return
	 */
	public Pager createPager() {	
		Pager pager = new Pager(1, 10);
		String pageNumber = getPara("pageNumber");
		String pageSize = getPara("pageSize");
		if(StrKit.notBlank(pageNumber)) {
			pager.setPageNumber(Integer.parseInt(pageNumber.trim()));
		}
		if(StrKit.notBlank(pageSize)) {
			pager.setPageSize(Integer.parseInt(pageSize.trim()));
		}
		return pager ;
	}
	
	
	/**
	 * 创建pager对象，封装请求参数paras
	 * @param paras
	 * @return
	 */
	public Pager createPager(String ...paras) {
		Pager pager = createPager();
		pager.setParams(getParas(paras));
		return pager ;
	}
	
	
	private Boolean toBoolean(String value, Boolean defaultValue) {
		if ((value == null) || ("".equals(value.trim())))
			return defaultValue;
		value = value.trim().toLowerCase();
		if (("1".equals(value)) || ("true".equals(value)))
			return Boolean.TRUE;
		if (("0".equals(value)) || ("false".equals(value)))
			return Boolean.FALSE;
		return Boolean.FALSE;
	}

	public Integer getParaToInteger(String name) {
		return Integer.parseInt(Mvcs.getReq().getParameter(name));
	}
	
	public Boolean getParaToBoolean(String name) {
		return toBoolean(Mvcs.getReq().getParameter(name), null);
	}

	public Boolean getParaToBoolean(String name, Boolean defaultValue) {
		return toBoolean(Mvcs.getReq().getParameter(name), defaultValue);
	}
	
}
